<?php
$this->title = "增加新文章";
?>
<div class="main">
	<div class="header">增加新文章</div>
	<div class="form-container">
		<form id="article-form" class="form-horizontal" action="<?=ROOT_URL?>article/articleadd" method="post" enctype="multipart/form-data">
			<div class="form-group">
				<label class="col-sm-1 control-label">文章标题</label>
				<div class="col-sm-4">
					<input id="title" type="text" class="form-control required" name="title">
				</div>
				<div class="errormsg col-sm-1"></div>
				<label class="col-sm-1 control-label">作者</label>
				<div class="col-sm-4">
					<input type="text" class="form-control" name="author">
				</div>
				<div class="errormsg col-sm-1"></div>
			</div>
			<div class="form-group">
				<label class="col-sm-1 control-label">关键字</label>
				<div class="col-sm-4">
					<input  type="text" class="form-control" name="keyword">
				</div>
				<div class="errormsg col-sm-1"></div>
				<label class="col-sm-1 control-label">模板</label>
				<div class="col-sm-4">
					<select class="form-control" name="template">
						<option value="article">缺省模板</option>
					</select>
				</div>
				<div class="errormsg col-sm-1"></div>
			</div>
			<div class="form-group">
				<label class="col-sm-1 control-label">审核</label>
				<div class="col-sm-4">
					<div class="radio col-sm-6">
						<label>
							<input type="radio" class="" name="audit" value="0">不需要审核
						</label>
					</div>
					<div class="radio col-sm-6">
						<label>
							<input type="radio" class="" name="audit" value="1" checked>需要审核
						</label>
					</div>
				</div>
				<div class="errormsg col-sm-1"></div>
				<label class="col-sm-1 control-label">重要度</label>
				<div class="col-sm-4">
					<select class="form-control" name="priority">
						<option value="一般" selected>一般</option>
						<option value="紧急">紧急</option>
						<option value="重要">重要</option>
					</select>
				</div>
				<div class="errormsg col-sm-1"></div>
			</div>
			<div class="form-group">
				<label class="col-sm-1 control-label">摘要</label>
				<div class="col-sm-4">
					<textarea class="form-control" name="abstract" rows=4></textarea>
				</div>
				<div class="errormsg col-sm-1"></div>
				<label class="col-sm-1 control-label">备注</label>
				<div class="col-sm-4">
					<textarea class="form-control" name="comment" rows=4></textarea>
				</div>
				<div class="errormsg col-sm-1"></div>
			</div>
			<div class="form-group">
				<label class="col-sm-1 control-label">栏目</label>
				<div class="col-sm-4">
					<select id="selcategory" class="form-control" multiple>
					<?php
					tree($category);
					function tree($tree){
						foreach ($tree as $value) {
							echo "<option style='padding-left:".($value['depth']*25)."px' value='".$value['id']."'>".$value['name']."</option>";

							if ($value['children'] != null) {
								tree($value['children']);
							}
						}
					}
					?>	
					</select>
					<input id="category" type="hidden" name="category" value="">
				</div>
				<div class="errormsg col-sm-1"></div>
				<label class="col-sm-1 control-label">可见组</label>
				<div class="col-sm-4">
					<select id="selgroups" class="form-control" multiple style="height:<?=count($group)*19+14?>px">
						<?php
						foreach ($group as $key => $value) {
							echo "<option value=". $value['id'] ." selected>". $value['name'] ."</option>";
						}
						?>
					</select>
					<input id="group" type="hidden" name="group" value="">
				</div>
				<div class="errormsg col-sm-1"></div>
			</div>
			<div class="form-group">
				<label class="col-sm-1 control-label">内容</label>
				<div class="col-sm-10">
					<script type="text/javascript" charset="utf-8" src="<?= Yii::getAlias('@web')?>/ueditor/ueditor.config.js"></script>
					<script type="text/javascript" charset="utf-8" src="<?= Yii::getAlias('@web')?>/ueditor/ueditor.all.min.js"> </script>
					<script id="editor" name="content" type="text/plain" style="width:100%;height:300px;"></script>
				</div>
			</div>
			<input id="rawcontent" type="hidden" name="rawcontent">
			<div class="form-group">
				<label class="col-sm-1 control-label">附件</label>
				<div id="file-container" class="col-sm-8">
					<div>
						<input name="attachfile" type="file" class="file"><input class="inputfile form-control" type="text" name="attachname"><input type="button" class="addfile btn btn-primary btn-sm" value="添加">
					</div>
				</div>
				<div class="errormsg col-sm-2"></div>
			</div>
		 	<div class="form-group">
				<label class="col-sm-4 control-label"></label>
				<div class="col-sm-4">
					<input id="submitbtn" type="button" class="btn btn-primary col-sm-4" value="确定" style="margin-right:40px">
					<input id="goback" type="button" class="btn btn-danger col-sm-4" value="返回">
				</div>
			</div> 
		</form>
	</div>
<div>
<script type="text/javascript">
var myeditor = UE.getEditor("editor");

$("#selcategory").css("height",($("#selcategory option").length*19+14)+"px");

$("#submitbtn").click(function(){
	if($.trim($("#title").val()) == ""){
		alert("请输入文章标题");
		return ;
	}

	var category = [];
	$("#selcategory option:selected").each(function(){
		category.push($(this).val());
	});
	if(category.length == 0)
	{
		alert("请至少选择一个栏目");
		return ;
	}
	$("#category").val(category.join(","));

	var groups = [];
	$("#selgroups option:selected").each(function(){
		groups.push($(this).val());
	});
	if(groups.length == 0)
	{
		alert("请至少选择一个可见组");
		return ;
	}
	$("#group").val(groups.join(","));

	$("#rawcontent").val(myeditor.getContentTxt());

	$("#article-form").submit();
});

$("#goback").click(function(){
	if(confirm("确定返回吗")){
		window.location.href = "<?=ROOT_URL?>article";
	}
});

$(".addfile").click(function(){
	var number = $(this).parents("#file-container").children("div").length;
	//alert(number);
	var str = "<div><input name='attachfile"+number+"' class='file' type='file'><input class='inputfile form-control' type='text' name='attachname"+number+"'><input type='button' class='delfile btn btn-danger btn-sm' value='删除'></div>";
	$(this).parents("#file-container").append(str);
});

$(document).on("click",".delfile",function(){
	if($(this).siblings(".file").val() != ""){
		if(confirm("确定删除该文件吗")){
			$(this).parent().remove();
		}
	}
	else
	{
		$(this).parent().remove();
	}
});

</script>
